package com.yimi.appstore.adapter;

import java.io.File;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.yimi.appstore.R;
import com.yimi.appstore.YimiApplication;
import com.yimi.appstore.download2.DownloadEntity;
import com.yimi.appstore.download2.DownloadManager;
import com.yimi.appstore.download2.FileService;
import com.yimi.appstore.util.BitmapManager;
import com.yimi.appstore.util.FileUtils;

public class DownloadExpandAdapter extends ExpandableAdapter<DownloadEntity> {

	BitmapManager bitmapManager;

	public DownloadExpandAdapter(Context context, List<DownloadEntity> listData) {
		super(context, listData);
		bitmapManager = new BitmapManager();
	}

	class ViewHolder {
		public ImageView appIcon;
		public TextView appName;
		public TextView appSizeStatus;
		public TextView downloadPercent;
		public View optionLayout;
		public ProgressBar progressBar;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		final DownloadEntity entity = listData.get(groupPosition);
		View row = convertView;
		final ViewHolder holder;
		if (row == null || row.getTag() == null) {
			holder = new ViewHolder();
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(R.layout.download_item_layout, null);
			holder.appIcon = (ImageView) row.findViewById(R.id.download_app_icon);
			holder.appName = (TextView) row.findViewById(R.id.download_app_name);
			holder.appSizeStatus = (TextView) row.findViewById(R.id.download_app_size_status);
			holder.downloadPercent = (TextView) row.findViewById(R.id.download_app_percent);
			holder.progressBar = (ProgressBar) row.findViewById(R.id.download_app_pb);
			holder.optionLayout = row.findViewById(R.id.download_options_rl);
			row.setTag(holder);
		} else {
			holder = (ViewHolder) row.getTag();
		}
		holder.progressBar.setVisibility(View.GONE);
		// if (entity.getIconUrl() != null) {
		// bitmapManager.loadBitmap(entity.getIconUrl(), holder.appIcon,
		// BitmapFactory.decodeResource(context.getResources(),
		// R.drawable.app_deafault));
		// } else {
		holder.appIcon.setBackgroundResource(R.drawable.app_deafault);
		// }
		holder.appName.setText(entity.getResource());
		holder.appSizeStatus.setText(FileUtils.convertStorage(entity.getTotalBytes()));
		holder.downloadPercent.setText("下载完成");
		return row;
	}

	@Override
	public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		final DownloadEntity entity = listData.get(groupPosition);
		View view = lInflater.inflate(R.layout.apk_installed_bottom_layout, null);
		view.findViewById(R.id.app_delete_btn).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				AlertDialog.Builder b = new Builder(context);
				View contentView = ((Activity) context).getLayoutInflater()
						.inflate(R.layout.delete_dialog_layout, null);
				final CheckBox checkBox = (CheckBox) contentView.findViewById(R.id.is_delete);
				b.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (checkBox.isChecked()) {
							File file = new File(entity.getLocalPath().replace(".tmp", ""));
							if (file.exists())
								file.delete();
						}
						FileService fileService = new FileService(context);
						fileService.deleteDownloadHistory(entity.getUrl());
						YimiApplication.downloadHistoryList.remove(entity);
						notifyDataSetChanged();
					}
				});
				b.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
				b.setIcon(R.drawable.ssl_icon);
				b.setTitle("温馨提示");
				b.setView(contentView);
				b.create().show();

			}
		});
		return view;
	}

}
